﻿using System;
using System.Collections.Generic;
using Kwd.Summary.Models;
using Kwd.Summary.Services;
using Kwd.Summary.ViewModels;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentManagement.ViewModels;
using Orchard.Core.Common.Settings;
using Orchard.Logging;

namespace Kwd.Summary.Module.Settings
{
  /// <summary>
  /// Add default settings for content part: 
  /// </summary>
  public class  SummarySettingsHook : ContentDefinitionEditorEventsBase
  {
    private IStrategyFactory _strategies;
    
    public SummarySettingsHook(IStrategyFactory strategies)
    {
      _strategies = strategies;
      Logger = NullLogger.Instance;
    }

    public ILogger Logger { get; set; }

    #region Settings for all Summary Parts.
    public override IEnumerable<TemplateViewModel> PartEditor(ContentPartDefinition definition)
    {
      //only for my part.
      if (definition.Name != typeof(SummaryPart).Name) yield break;

      var model = definition.Settings.GetModel<SummaryPartSettings>();
      if (String.IsNullOrWhiteSpace(model.DefaultFlavor))
      { model.DefaultFlavor = BodyPartSettings.FlavorDefaultDefault; }

      yield return DefinitionTemplate(model);
    }

    public override IEnumerable<TemplateViewModel> PartEditorUpdate(ContentPartDefinitionBuilder builder, IUpdateModel updateModel)
    {
      if (builder.Name != typeof(SummaryPart).Name) yield break;

      var model = new SummaryPartSettings();
      if (updateModel.TryUpdateModel(model, typeof(SummaryPartSettings).Name, null, null))
      {
        builder.WithSetting(typeof(SummaryPartSettings).Name + ".DefaultFlavor", model.DefaultFlavor);
      }

      yield return DefinitionTemplate(model);
    }
    #endregion

    #region Settings for summary part assigned to a content type.
    public override IEnumerable<TemplateViewModel> TypePartEditor(ContentTypePartDefinition definition)
    {
      if (definition.PartDefinition.Name != typeof(SummaryPart).Name) yield break;

      var model = new SummaryTypePartSettingsViewModel(definition.Settings.GetModel<SummaryTypePartSettings>());

      if (String.IsNullOrWhiteSpace(model.DefaultStrategy)) {
        model.DefaultStrategy = _strategies.Default.GetType().Name;
      }

      if (String.IsNullOrWhiteSpace(model.Flavor)){
        model.Flavor = definition.PartDefinition.Settings.GetModel<SummaryPartSettings>().DefaultFlavor;
      }

      model.StrategyOptions = new StrategyOptions().Build(_strategies.List(), model.DefaultStrategy);


      yield return DefinitionTemplate(model, typeof(SummaryTypePartSettings).Name, typeof(SummaryTypePartSettings).Name);
    }

    public override IEnumerable<TemplateViewModel> TypePartEditorUpdate(ContentTypePartDefinitionBuilder builder, IUpdateModel updateModel)
    {
      if (builder.Name != typeof(SummaryPart).Name) yield break;
      
      var model = new SummaryTypePartSettingsViewModel();

      if (updateModel.TryUpdateModel(model, typeof(SummaryTypePartSettings).Name, null, new string[] { "StrategyOptions" }))
      {
        builder
          .WithSetting(typeof(SummaryTypePartSettings).Name + ".DefaultStrategy", model.DefaultStrategy)
          .WithSetting(typeof(SummaryTypePartSettings).Name + ".Flavor", model.Flavor);
      }
      else 
      {
        if(String.IsNullOrWhiteSpace(model.DefaultStrategy)){
          model.DefaultStrategy = _strategies.Default.GetType().Name;
        }

        model.StrategyOptions = new StrategyOptions().Build(_strategies.List(), model.DefaultStrategy);
      }

      yield return DefinitionTemplate(model, typeof(SummaryTypePartSettings).Name, typeof(SummaryTypePartSettings).Name);
    }
    #endregion
  }
}